package com.ruoyi.project.module.connector.controller;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.module.project.domain.Project;
import com.ruoyi.project.module.project.service.IProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.module.connector.domain.Connector;
import com.ruoyi.project.module.connector.service.IConnectorService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

import javax.websocket.server.PathParam;

/**
 * 接口模块 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-10-02
 */
@Controller
@RequestMapping("/module/connector")
public class ConnectorController extends BaseController
{
    private String prefix = "module/connector";
	
	@Autowired
	private IConnectorService connectorService;

	@Autowired
	private IProjectService projectService;

	@RequiresPermissions("module:connector:view")
	@GetMapping()
	public String connector()
	{
	    return prefix + "/connector";
	}

	@RequiresPermissions("module:connector:view")
	@GetMapping("/uniqueid/{uniqueid}")
	public String connector(@PathVariable("uniqueid") Integer uniqueid, ModelMap mmap)
	{
	    mmap.put("uniqueid",uniqueid);
		return prefix + "/connector";
	}

	/**
	 * 查询接口模块列表
	 */
	@RequiresPermissions("module:connector:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Connector connector)
	{
		startPage();
        List<Connector> list = connectorService.selectConnectorList(connector);
		return getDataTable(list);
	}
	
	/**
	 * 新增接口模块
	 */
	@GetMapping("/add/{parentId}")
	public String add(@PathVariable("parentId") Integer parentId, ModelMap mmap)
	{
		Project info = projectService.selectProjectById(parentId);
		mmap.put("project", info);
		Connector connector=new Connector();
		String projectname = info.getProjectname();
		if(projectname !=null){
			String[] s = projectname.split("[_-]");
			String interfacename="";
			StringBuilder urlPath=new StringBuilder();
			if(s.length>0){
				int i=s.length;
				i--;
				interfacename=s[i];
				for (i--; i >=0; i--) {
					urlPath.insert(0,s[i]).insert(0,"/");
				}

			}
			connector.setUrlContextpath(urlPath.toString());
			connector.setInterfacename(interfacename);
		}
		mmap.put("connector", connector);
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存接口模块
	 */
	@RequiresPermissions("module:connector:add")
	@Log(title = "接口模块", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Connector connector)
	{
		if(StringUtils.isBlank(connector.getMethod())
				||StringUtils.isBlank(connector.getInterfacename())
				||StringUtils.isBlank(connector.getDescription())
				||connector.getApiProjectnameId()==null
				){
			return error(1, "所属项目,接口名称,接口描述,请求类型不允许为空");
		}
		return toAjax(connectorService.insertConnector(connector));
	}

	/**
	 * 修改接口模块
	 */
	@GetMapping("/edit/{apiConnectorId}")
	public String edit(@PathVariable("apiConnectorId") Integer apiConnectorId, ModelMap mmap)
	{
		Connector connector = connectorService.selectConnectorById(apiConnectorId);
		mmap.put("connector", connector);
		Project info = projectService.selectProjectById(connector.getApiProjectnameId());
		mmap.put("project", info);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存接口模块
	 */
	@RequiresPermissions("module:connector:edit")
	@Log(title = "接口模块", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Connector connector)
	{		
		return toAjax(connectorService.updateConnector(connector));
	}
	
	/**
	 * 删除接口模块
	 */
	@RequiresPermissions("module:connector:remove")
	@Log(title = "接口模块", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(connectorService.deleteConnectorByIds(ids));
	}
	
}
